
coos.form.input = function(){};

coos.form.input.radio = function(name)
{
	var rs = coos.$name(name);
	var len = rs.length;
	for(var i = 0; i < len; i++)
	{
		if(rs[i].checked)
		{
			return rs[i];
		}
	}
	return null;
};

coos.form.checkbox = function(){};

coos.form.checkbox.getElements = function(formElement)
{
	var form = coos.$obj(formElement) || document;
    return form.getElementsByTagName("input"); 
};

coos.form.checkbox.isSelect = function(formElement) 
{
	var els = coos.form.checkbox.getElements(formElement);
    var len = els.length;
    for (var i = 0; i < len; i++)
    {
		if(els[i].checked == true)
		{
       		return true;
		}
    }
    return false;
};

coos.form.checkbox.selectAll = function(formElement) 
{
	var els = coos.form.checkbox.getElements(formElement);
    var len = els.length;
    for (var i = 0; i < len; i++)
    {
		if(els[i].type == "checkbox")
		{
       		els[i].checked = true;
		}
    }
};

coos.form.checkbox.unselectAll = function(formElement) 
{
	var els = coos.form.checkbox.getElements(formElement);
    var len = els.length;
    for (var i = 0; i < len; i++)
    {
		if(els[i].type == "checkbox")
		{
       		els[i].checked = false;
		}
    }
};

coos.form.checkbox.reverse = function(formElement) 
{
	var els = coos.form.checkbox.getElements(formElement);
    var len = els.length;
    for (var i = 0; i < len; i++)
    {
		if(els[i].type == "checkbox")
		{
			els[i].checked = !els[i].checked;
		}
    }
};

coos.form.checkbox.values = function(formElement) 
{
	var els = coos.form.checkbox.getElements(formElement);
    var len = els.length;
    var s = "";
    for (var i = 0; i < len; i++)
    {
		if(els[i].type == "checkbox")
		{
			if (els[i].checked)
			{
				s += els[i].name + "=" + els[i].value + "&";
			}
		}
    }
    return s;
};

coos.form.validate.htmltotext = function(html)
{
	return html.replace(/<[^>]*>/g,"");
};

coos.form.validate.limitlength = function(obj,length)
{
	if(obj.value.length > length)
	{
		obj.value = obj.value.substring(0,length);
		alert("长度不能超过" + length + "个字符！");
	}
};

/**
 * 验证单个表单项不能为空
 */
coos.form.validate.empty = function(formId,fieldName,message)
{
	var message = message || "内容不能为空，请填写后提交！";
	var form = coos.$id(formId);
	if(form[fieldName].value.trim() == "")
	{
		alert(message);
		form[fieldName].focus();
		return false;
	}
	return true;
};

/**
 * 批量验证表单项不能为空
 */
coos.form.validate.emptys = function(formId,fieldNames,messages)
{
	var message = "内容不能为空，请填写后提交！";
	var form = coos.$id(formId);
	for(var i=0;i<fieldNames.length; i++)
	{
		if(form[fieldNames[i]] && form[fieldNames[i]].value.trim() == "")
		{
			if(messages && messages[i])
			{
				alert(messages[i]);
			}
			else
			{
				alert(message);
			}
			form[fieldNames[i]].focus();
			return false;
		}
	}
	return true;
};

/**
 * 验证表单
 * coos.form.validate.regex("pageForm","clothStockIn.discount","^[0-9]+.?[0-9]*$","必须为数字");
 */
coos.form.validate.regex = function(formId,fieldName,regex,message)
{
	var message = message || "输入格式不对，请改正后提交！";
	var form = coos.$id(formId);
	var oReg = new RegExp(regex);
	var value = form[fieldName].value.trim();
	if(value != "" && !oReg.test(value))
	{
		alert(message);
		form[fieldName].focus();
		return false;
	}
	return true;
};

/**
 * 批量验证表单
 * coos.form.validate.regexs("pageForm",["clothStockIn.discount","clothStockIn.quantity"],["^[0-9]+.?[0-9]*$","^[1-9]+[0-9]*]*$"],["必须为数字","必须为整数");
 */
coos.form.validate.regexs = function(formId,fieldNames,regexs,messages)
{
	var message = message || "输入格式不对，请改正后提交！";
	var form = coos.$id(formId);	
	for(var i = 0; i < fieldNames.length; i++)
	{
		var oReg = new RegExp(regexs[i]);
		if(form[fieldNames[i]])
		{
			var value = form[fieldNames[i]].value.trim();
			if(value != "" && !oReg.test(value))
			{
				if(messages && messages[i])
				{
					alert(messages[i]);
				}
				else
				{
					alert(message);
				}
				form[fieldNames[i]].focus();
				return false;
			}
		}
	}
	return true;
};

coos.form.validate.data = function(data)
{
	var pass = false;
	if(data["unEmptyNames"].length > 0)
	{
		pass = coos.form.validate.emptys(data["formId"],data["unEmptyNames"],data["unEmptyMessages"]);
	}
	else
	{
		pass = true;
	}
	if(pass && data["regexNames"].length > 0)
	{
		pass = coos.form.validate.regexs(data["formId"],data["regexNames"],data["regexs"],data["regexMessages"]);
	}
	return pass;
};

/**
 * 通过传入json格式的数据,验证后提交
 * @example
 * function add()
	{
		var data ={
			formId          : "pageForm",
			unEmptyNames    : ["clothStockIn.lotNo","clothStockIn.quantity","clothStockIn.unitPrice"],
			unEmptyMessages : ["Lot No不能为空","存入数量不能为空","单价不能为空"],
			regexNames      : ["clothStockIn.quantity","clothStockIn.unitPrice","clothStockIn.discount"],
			regexs          : ["^[0-9]+.?[0-9]*$","^[0-9]+.?[0-9]*$","^[0-9]+.?[0-9]*$"],
			regexMessages   : ["存入数量必须为数字","单价必须为数字","折扣必须为数字"],
			end             : true
		};
		coos.form.validate.submit(data);
	}
 
 */
coos.form.validate.submit = function(data)
{
	var pass = false;
	pass = coos.form.validate.data(data);
	if(pass)
	{
		coos.$id(data["formId"]).submit();
		return false;
	}
};

/**
 * 光标移动到文本域清除默认提示，如果没有输入内容光标离开重新恢复默认提示。
 * onFocus="coos.form.clearDefaultText(this,'我来说两句')"，必须用onFocus再重新获得焦点时才起作用
 */
coos.form.clearDefaultText = function(el,message)
{
	var obj = coos.$obj(el);
	if(obj.value == message)
	{
		obj.value = "";
	}
	obj.onblur = function()
	{
		if(obj.value == "")
		{
			obj.value = message;
		}
	}
};
